草庐IT

Rust 结构体

全部标签

json - 遍历嵌套的 JSON 结构

我想遍历嵌套的JSON结构并从接口(interface)中获取每个键和值{}http://play.golang.org/p/B-B3pejGJW所以我想从下面的结构{"tg":{"A":{"E":100,"H":14},"B":{"D":1},"C":{"D":1,"E":1},"D":{"F":1,"G":1,"H":1},"E":{"G":1}}}我能够得到以下内容a:=js.Get("tg").Get("D").Get("F")fmt.Println(*a)//{1}但在将此类型断言为整数时遇到问题。invalidtypeassertion:(*a).(int)如何遍历整个结构

reflection - 设置作为接口(interface)传递的任何结构的变量{}

我想知道如何在使用interface{}值时使用反射设置变量,并且所有类型的结构都可以传递给funcF(ointerface{})。如何将第一个值(s.A)更改为'hello'?packagemainimport("fmt""reflect")typeTstruct{Astring}funcmain(){F(T{"foo"})}funcF(ointerface{}){t:=reflect.ValueOf(&T{"bar"}).Elem()s:=reflect.ValueOf(&o).Elem()//okfmt.Println("struct:",t.Field(0).CanSet())

struct - 在结构或接口(interface)内部执行计算?

我正在构建一个数据以保存到mongodb。我有一个来自这样的API的json响应{coord:{lon:20,lat:30}main:[{"temp":304.15,"pressure":1005,"humidity":74,"temp_min":304.15,"temp_max":304.15}]}在main[0].temp、main[0].temp_min、main[0].temp_max中,值以开尔文为单位。我想在将其保存为mongodb之前将其转换为摄氏温度。我可以像这样制作一个简单的结构:typeItemstruct{TempstringPressureintHumidity

json - 如何将 map[string]interface{} 转换为不同类型的结构?

我正在调用一个API,它将像这样返回Json对象:{name:"XXX"type:"TYPE_1"shared_fields:{...}type_1_fields:{...}..type_2_fields:{...}}根据不同的类型,这个对象会有不同种类的字段,但是这些字段对于不同的类型是一定的。因此,我将Json字符串解码为map[string]interface{}以获取不同的类型,但是如何将这些map[string]interface{}转换为某个结构?varfmap[string]interface{}err:=json.Unmarshal(b,&f)type:=f["type

reflection - 使用(相对)未知/任意方法扩展结构,进行反射(或避免反射)

下面显然不起作用:Arbitrary:=struct{field1stringfield2string}{"a","b"}fmap:=make(map[string]func(string)string)fmap["fone"]=func(sstring)string{fmt.Printf("functionfone:%s",s)}fmap["ftwo"]=func(sstring)string{fmt.Printf("functionftwo:%s",s)}//probablyok,assimpleexamplesgo,tothispointwherereflectionneedst

go - 范围超过结构数组的地址

我有一个[]Struct类型的结构数组。当我以以下形式覆盖它时:fori,val:=rangemystructarray我知道val是一个局部变量,它包含mystructarray[i]的副本。有没有比这更好的遍历mystructarray的地址的方法:fori:=rangemystructarray{valptr=&mystructarray[i]}? 最佳答案 在接收到slice内容的指针时无法进行迭代(当然,除非它是一个指针slice)。你的例子是最好的方式:fori:=rangemySlice{x=&mySlice[i]//

interface - 我如何在 Go 中将 interface{} 的一部分转换为我的结构类型的一部分?

这个问题在这里已经有了答案:Typeconvertingslicesofinterfaces(9个回答)关闭3年前。funcGetFromDB(tableNamestring,m*bson.M)interface{}{var(__session*mgo.Session=getSession())//ifthequeryargisnil.giveitthenullqueryifm==nil{m=&bson.M{}}__result:=[]interface{}{}__cs_Group:=__session.DB(T_dbName).C(tableName)__cs_Group.Find(

Go:将空结构编码为 json

我正在尝试将结构编码为json。它在结构具有值时起作用。但是,当结构没有值时,我无法访问网页:开始:typeFruitsstruct{Apple[]*Description'json:"apple,omitempty"'}typeDescriptionstruct{ColorstringWeightint}funcHandler(whttp.ResponseWriter,r*http.Request){j:={[]}js,_:=json.Marshal(j)w.Write(js)}错误是因为json.Marshal无法编码空结构吗? 最佳答案

file - 在 Go 中将多个结构写入一个文件

我和我的团队是Go的新手,我们有一个“Header”结构和多个我们试图写入文件的“Record”结构。但是,每当我们尝试通过重写来更新文件中的Header结构时,文件的其余部分就会变得一团糟。我们正在使用编码/解码:(数据文件从os.Open返回)dataFile.Seek(header.FreePtr,0)//seektofreespace-couldwejustrefactorandseektoendoffile?encoder:=gob.NewEncoder((dataFile))err=encoder.Encode(record)iferr!=nil{panic(err)}da

pointers - Golang 结构问题指向父结构方法

我在Golang中遇到了如下问题:packagemainimport"fmt"typeFoostruct{namestring}typeBarstruct{Fooidstring}func(f*Foo)SetName(namestring){f.name=name}func(f*Foo)Name()string{returnf.name}funcmain(){f:=&Foo{}f.SetName("SetFooname")fmt.Println("GetfromFoostructname:",f.Name())bar:=&Bar{Foo:Foo{name:"SetFoonamefrom